#ifndef MASSAGESTYPES_CPP
#define MASSAGESTYPES_CPP

#include "MassagesTypes.h"
#include <QDebug>



//player info
sf::Packet cr_PlInfo_msg(sf::IpAddress ip, sf::Uint16 port, QString name) {

    sf::Packet pack;
    pack << (uchar)'p';    //Player

    sf::Int16 size = name.size();
    pack << size;

    QChar* data = name.data();
    while (!data->isNull()) {
        pack << data->unicode();
        ++data;
    }

    pack << ip.toInteger();

    pack << port;

    return pack;
}
void open_PlInfo_msg(sf::Packet &pack, sf::IpAddress &ip, sf::Uint16 &port, QString &name) {
    open_msg_string(pack, name);

    sf::Uint32 adrInt;
    pack >> adrInt;
    ip = sf::IpAddress(adrInt);

    pack >> port;
}


//server info
sf::Packet cr_ServInfo_msg(QString name, sf::Uint8 plCount) {

    sf::Packet pack;
    pack << (uchar)'s';    //Server

    sf::Int16 size = (sf::Int16)name.size();
    pack << size;

    QChar* data = name.data();
    while (!data->isNull()) {
        pack << data->unicode();
        ++data;
    }

    pack << plCount;

    return pack;
}

void open_ServInfo_msg(sf::Packet &pack, QString &name, sf::Uint8 &plCount) {

    sf::Int16 size;
    pack >> size;
    ushort ch;

    for (int i = 0; i < size; i++) {
        pack >> ch;
        name += ch;
        qDebug() << ch << " -> " << name;
    }

    pack >> plCount;
    qDebug() << "count " << plCount;

}

//name request
sf::Packet cr_Name_msg(sf::IpAddress ip, sf::Uint16 port) {

    sf::Packet pack;
    pack << (uchar)'n';    //Name
    pack << ip.toInteger();

    pack << port;

    return pack;
}

//connect request
sf::Packet cr_Connect_msg() {

    sf::Packet pack;
    pack << (uchar)'c';    //Connect

    return pack;
}

//connect request
sf::Packet cr_IsServer_msg() {

    sf::Packet pack;
    pack << (uchar)'?';    //Are u server?

    return pack;
}

//name request
sf::Packet cr_Ok_msg() {

    sf::Packet pack;
    pack << (uchar)'k';    //oK

    return pack;
}


//chat message
sf::Packet cr_Chat_msg(sf::IpAddress adr, sf::Uint16 port, QString msg) {

    sf::Packet pack;
    pack << (uchar)'m';    //Msg

    sf::Int16 size = (sf::Int16)msg.size();
    pack << size;

    QChar* data = msg.data();
    while (!data->isNull()) {
        pack << data->unicode();
        ++data;
    }

    pack << adr.toInteger();
    pack << port;

    return pack;
}


void open_msg_string(sf::Packet &pack, QString &msg) {
    sf::Int16 size;
    pack >> size;
    qDebug() << "size: " << size;
    ushort ch;

    for (int i = 0; i < size; i++) {
        pack >> ch;
        msg += ch;
    }

}

void open_msg_adr(sf::Packet &pack, sf::IpAddress &ip, sf::Uint16 &port) {

    sf::Uint32 whosInt;
    pack >> whosInt;
    ip = sf::IpAddress(whosInt);

    pack >> port;
}

//player list msgs
sf::Packet cr_PlListReq_msg() {

    sf::Packet pack;
    pack << (uchar)'l';    //List

    return pack;
}

sf::Packet cr_Go_msg() {
   sf::Packet pack;
   pack << (uchar)'g';    //Go

   return pack;
}

sf::Packet cr_PlList_msg(std::vector<plInLobby>* list) {

    sf::Packet pack;
    pack << (uchar)'t';    //players lisT

    pack << (sf::Int16)list->size();

    std::vector<plInLobby>::iterator it = list->begin();
    for (; it != list->end(); it++) {
        pack << (*it).adr.toInteger();
        pack << (*it).port;
    }

    return pack;
}

void open_PlList_msg(sf::Packet &pack, std::vector<plInLobby>* list) {

    while (list->size() > 0)
        list->pop_back();

    sf::Int16 count;
    pack >> count;

    plInLobby player;
    sf::Uint32 adrInt;
    sf::Uint16 plPort;

    for (sf::Int16 i = 0; i < count; i++) {
        pack >> adrInt;
        pack >> plPort;

        player.name = "";
        player.adr = sf::IpAddress(adrInt);
        player.port = plPort;

        list->push_back(player);
    }
}

//id
sf::Packet cr_id_msg(sf::Int16 id) {

    sf::Packet pack;
    pack << (uchar)'i';    //Id
    pack << id;

    return pack;
}

//Panzer
sf::Packet cr_Tank_msg(sf::Int8 hp, sf::Uint16 x, sf::Uint16 y, sf::Uint16 alpha, sf::Uint16 beta, sf::Int16 color) {

    sf::Packet pack;
    pack << (uchar)'z';    //panZer

    pack << hp;
    pack << x;
    pack << y;
    pack << alpha;
    pack << beta;
    pack << color;

    return pack;
}


void open_Tank_msg(sf::Packet &pack, tankInfo &tank) {

    pack >> tank.hp;
    pack >> tank.x;
    pack >> tank.y;
    pack >> tank.alpha;
    pack >> tank.beta;
    pack >> tank.color;
}

//Shoot
sf::Packet cr_Shoot_msg(sf::Uint16 x, sf::Uint16 y, sf::Uint16 alpha, sf::Int16 color, sf::Int16 id) {

    sf::Packet pack;
    pack << (uchar)'o';    //shOOt

    pack << x;
    pack << y;
    pack << alpha;
    pack << color;
    pack << id;

    return pack;
}

void open_Shoot_msg(sf::Packet &pack, shoot &sh) {

    pack >> sh.x;
    pack >> sh.y;
    pack >> sh.alpha;
    pack >> sh.color;
    pack >> sh.id;
}



#endif // MASSAGESTYPES_CPP
